# DEPRECATED

import math

import numpy as np
import numpy.ma as ma

import matplotlib
rcParams = matplotlib.rcParams
from matplotlib.artist import kwdocd
from matplotlib.axes import Axes
from matplotlib import cbook
from matplotlib.patches import Circle
from matplotlib.path import Path
from matplotlib.ticker import Formatter, Locator, NullLocator, FixedLocator, NullFormatter
from matplotlib.transforms import Affine2D, Affine2DBase, Bbox, \
    BboxTransformTo, IdentityTransform, Transform, TransformWrapper

class AuxTransformAxes(Axes):

    def __init__(self, parent_axes, aux_transform):
        self._parent_axes = parent_axes
        self.transAux = aux_transform
        
        Axes.__init__(self,
                      parent_axes.figure,
                      parent_axes._position, frameon=False)
        self.set_xticks([])
        self.set_yticks([])

    def _set_lim_and_transforms(self):

        Axes._set_lim_and_transforms(self)
        
        self.transAxes = self._parent_axes.transAxes

        # The complete data transformation stack -- from data all the
        # way to display coordinates
        self.transData = \
            self.transAux + \
            self._parent_axes.transData



class AuxTransform(Transform):
    """
    The base Aitoff transform.
    """
    input_dims = 2
    output_dims = 2
    is_separable = False

    def __init__(self, resolution):
        """
        Create a new Aitoff transform.  Resolution is the number of steps
        to interpolate between each input line segment to approximate its
        path in curved Aitoff space.
        """
        Transform.__init__(self)
        self._resolution = resolution

    def transform(self, ll):
        x = np.asarray(ll[:,0:1])
        y = np.asarray(ll[:,1:2])

        return np.concatenate((x+y, y), 1)
        #return ll

    transform.__doc__ = Transform.transform.__doc__

    transform_non_affine = transform
    transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__

    def transform_path(self, path):
        # do we need curved path???
        vertices = path.vertices
        ipath = path.interpolated(self._resolution)
        return Path(self.transform(ipath.vertices), ipath.codes)
    transform_path.__doc__ = Transform.transform_path.__doc__

    transform_path_non_affine = transform_path
    transform_path_non_affine.__doc__ = Transform.transform_path_non_affine.__doc__

    def inverted(self):
        return InvertedAuxTransform(self._resolution)
    inverted.__doc__ = Transform.inverted.__doc__

class InvertedAuxTransform(Transform):
    input_dims = 2
    output_dims = 2
    is_separable = False

    def __init__(self, resolution):
        Transform.__init__(self)
        self._resolution = resolution

    def transform(self, xy):
        # MGDTODO: Math is hard ;(
        return xy
    transform.__doc__ = Transform.transform.__doc__

    def inverted(self):
        return SimpleTransform(self._resolution)
    inverted.__doc__ = Transform.inverted.__doc__

#register_projection(WcsAxes)

# Now make a simple example using the custom projection.
from pylab import *

clf()

f1 = figure(1)
f1.clf()
#f2 = figure(2)
ax_parent = f1.add_subplot(111)

RESOLUTION = 75
tr = AuxTransform(RESOLUTION)

ax = AuxTransformAxes(ax_parent, tr)
#ax = Axes(ax_parent.figure, ax_parent._position)
f1.add_axes(ax)
#p1 = ax_parent.plot([-1, 0, 1], [-1, 0, 1], "-")
p2 = ax.plot([-.5, .5, .5], [-.5, -.5, .5], "-")
#p = plot([-1, 1, 1], [-1, -1, 1], "-")

ax_parent.set_xlim(-2, 2)
ax_parent.set_ylim(-2, 2)


x = arange(0, 1, 0.1)
y = arange(0, 1, 0.1)
x = array([0., 0.2, 0.4, 0.6])
y = array([0., 0.2, 0.4, 0.6])
z = arange(9).reshape([3, 3])
ax.pcolormesh(x, y, z)

#p1._transformed_path._path
draw()
